from com import skype

from sj.plugin import SkypeJython

from sj.utils import parseCmdln, getChatList
from sj.utils import examine as MyExamine

import sys

DefaultOptions = {'topic': 'Jython',
                  'user_id': '',
                  'windows': False,
                  'inspect': False,
                  'no_shell': False,
                  'plugin_id': '',
                  'show_options': False,
                  'store_connector_events': False,
                  'globals_module': 'sj.plugin'}

def main(argv = None):
    options = parseCmdln(argv, **DefaultOptions)
    skype.Skype.setDaemon(False)

    try:
        if options.show_options:
            print options

        if options.windows:
            print getChatList()
            # XXX: shutdown skype connection??
            return

        if not options.no_shell:
            global shell
            shell = SkypeJython(options)

        if options.inspect:
            if options.no_shell:
                ns_locals = {}
                ns_globals = globals()
            else:
                ns_locals = shell.getLocalNamespace()
                ns_globals = shell.getGlobalNamespace()

            # interact(ns_locals, ns_globals)
            MyExamine(ns_locals, ns_globals)
            sys.exit(0)

        else:
            try: from org.fraun import SkypeJython as ResourceClass
            except ImportError: pass
            else: shell.setupSystemTray(ResourceClass.getClass())

    except skype.NotAttachedException:
        print '** Not Attached'

if __name__ == '__main__':
    main()
